
import axios from 'axios'
import util from '../../util/util'
import userTable from '../../api/user'
const Mock = require('mockjs');
/**
 * 使用MOCK生成模拟数据
 * Mock.mock(url, dataTmp)
 * */
Mock.mock(/login.ujson/, 'post', function(options){
    // 模拟服务端接受客户端数据请求处理
    let sessionID = util.Cookie.get('VUEID');
    let serverToken = userTable[sessionID].token;
    let serverEmail = userTable[sessionID].email;
    let clientData = JSON.parse(options.body);
    
    if(serverEmail == clientData.email) {
        if(serverToken == clientData.token) {
            return {stateCode: 1, msg: '登录成功'}
        } else {
            return {stateCode: 0, msg: 'Token已过期'}
        }
    } else {
        return {stateCode: 0, msg: '用户不存在'}
    }
});


const state = {
    login_email: '',
    login_token: '',
    login_name: '',
}

const getters = {
    currentUser: state => {
        return {
            email: state.login_email,
            token: state.login_token,
            name: state.login_name
        }
    }
}

const mutations = {
    getLocalUser (state) {
        state.login_email = sessionStorage.getItem('email');
        state.login_token = sessionStorage.getItem('token');
        state.login_name = sessionStorage.getItem('name');
    },
    setUser (state, payload) {
        state.login_email = payload.result.email
        state.login_token = payload.result.token
        state.login_name = payload.result.name
    },
    logout (state) {
        sessionStorage.removeItem('email')
        sessionStorage.removeItem('token')
        sessionStorage.removeItem('name')
        state.login_email = ''
        state.login_token = ''
        state.login_name = ''
    }
}

const actions = {
    /**
     * Login
     * new Promise((resolve, reject) => {})
     * Authorization: 'Bearer ' + token
     * email: payload.email
     * pass: payload.pass
     * name: payload.name
     */
    // login ({ commit }, payload) {
    //     axios.post('https://douban.herokuapp.com/user/', 
    //     {
    //         email: payload.email,
    //         Authorization: payload.Authorization
    //     }).then(function(result){
    //         console.log(result)
    //         debugger;
    //     }).catch(function(err){
    //         console.log(err);
    //     })
    // },
    login ({commit}, payload) {
        debugger;
        return axios.post('login.ujson', {
            email: payload.email,
            token: payload.token
        }).then(function(result) {
            console.log(result.data);
            return result.data;
        }).catch(function(err){
            console.log(err);
        })
    },
    /**
     * Register
     * new Promise((resolve, reject) => {})
     * email: payload.email
     * pass: payload.pass
     * name: payload.name
     */
    register({commit}, payload) {
        return axios.post('https://douban.herokuapp.com/user/', {
            email: payload.email,
            pass: payload.pass,
            name: payload.name
        }).then(function(result){
            commit('setUser', {
                result: result.data
            })
            return result.data;
        }).catch(function(err) {
            console.log(err);
        })
    }
}

export default  {
    state,
    getters, 
    mutations,
    actions
}